package gspushmq.v1.app.subscribeapp.utils;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import ggframework.mongodb.BasicDBObject;
import ggframework.mongodb.DBCollection;
import ggframework.mongodb.DBCursor;
import ggframework.mongodb.DBObject;
import ggframework.util.GGConfigurer;
import ggframework.util.GGLogger;
import gogoal.api.GoGoalAPI;
import gspushmq.util.MongoUtil;

/**
 * @Description APP推送帮助类
 * @Author chengsj
 * @Date 2018/10/29 10:02
 * @Version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2015 SunTime Co'Ltd Inc. All rights reserved.
 **/
public class GGPushUtil {


    /**
     * @param ids 大小不限
     * @Description 根据ids集合远程获取用户账户名
     * @Author chengsj
     * @Version 1.0
     */
    public static List<Map<String, String>> getUserAccountNames(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            return null;
        }
        List<Map<String, String>> reList = Lists.newArrayList();
        while (ids.size() > 0) {
            try {
                List<Map<String, String>> temList = getUserAccountNamesWithLimit(ids.size() > 200 ? ids.subList(0, 200) : ids);
                if (temList != null && temList.size() > 0) {
                    reList.addAll(temList);
                    ids.subList(0, ids.size() > 200 ? 200 : ids.size()).clear();
                }
            } catch (Exception e) {
                e.printStackTrace();
                GGLogger.error(GGPushUtil.class, "根据ids字符串调用对应用户信息接口异常！idstr=" + ids.subList(0, 200));
            }

        }
        return reList;

    }


    /**
     *
     * @description 根据账户id集合，批量获取账户名集合
     * @param uids 账户id集合
     * @version 1.0
     * @author chengsja
     * @date 2019/1/2 15:23
     *
     */
    public static Set<String> getUserAccountName(Set<Long> uids){
        if (uids == null || uids.size() ==0){
            return null;
        }
        Set<String> accountNames = Sets.newHashSet();
        DBCollection collection = MongoUtil.getFtAccountCollection("tau_account");
        DBObject query = new BasicDBObject();
        DBObject field = new BasicDBObject();
        query.put("account_id",new BasicDBObject("$in",uids));
        field.put("account_name",1);
        DBCursor cursor = collection.find(query,field);
        while (cursor.hasNext()){
            DBObject o = cursor.next();
            accountNames.add(o.getString("account_name"));
        }
        cursor.close();
        return accountNames;
    }


    /**
     * @Description 远程批量获取用户名，根据ids集合分批远程获取用户账户名, http请求限制大小limit < 4K
     * @param ids 大小限制为<= 200
     * @Author chengsj
     * @Version 1.0
     */
    private static List<Map<String, String>> getUserAccountNamesWithLimit(List<String> ids) throws Exception {
        List<Map<String, String>> reList = Lists.newArrayList();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < ids.size(); i++) {
            buffer.append(ids.get(i)).append(";");
        }
        String str = buffer.toString();
        str = str.substring(0, str.length() - 1);
        String usersUrl = "v1/gguser_info/get_list";
        Map<String, String> parms = Maps.newHashMap();
        parms.put("account_ids", str);
        String jsonUsers = GoGoalAPI.get(GGConfigurer.get("user.api.domain"), GGConfigurer.get("user.app_key"), GGConfigurer.get("user.api.appSecret"), usersUrl, parms);
        if (StringUtils.isBlank(jsonUsers)) {
            return null;
        }
        JSONObject mess = (JSONObject) JSONObject.parse(jsonUsers);
        JSONArray users = mess.getJSONArray("data");
        if (users == null) {
            return null;
        }
        for (Object o : users) {
            String id = ((JSONObject) o).getString("account_id");
            String account = ((JSONObject) o).getString("account");
            if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(account)) {
                Map<String, String> m = Maps.newHashMap();
                m.put("account_id", id);
                m.put("account_name", account);
                reList.add(m);
            }
        }
        return reList;
    }

    /**
     * 账户名合并，每个字符串支持不大于99个账号合并
     * Created by chengsj on 2018/10/29
     */
    public static List<String> accountsMergeLimt100(List<String> accountNameSet) {
        if (accountNameSet == null || accountNameSet.size() == 0) {
            return null;
        }
        Set<String> mergeSets = Sets.newHashSet();
        while (accountNameSet.size() > 0) {
            StringBuffer buffer = new StringBuffer();
            for (String s : accountNameSet.subList(0, accountNameSet.size() > 99 ? 99 : accountNameSet.size())) {
                buffer.append(s).append(",");
            }
            accountNameSet.subList(0, accountNameSet.size() > 99 ? 99 : accountNameSet.size()).clear();
            String str = buffer.toString();
            mergeSets.add(str.substring(0, str.length() - 1));
        }
        return Lists.newArrayList(mergeSets);
    }
}


